package com.maxuxu.scihome.controller;

import com.maxuxu.scihome.pojo.Message;
import com.maxuxu.scihome.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: LoginController
 * @author: Maxuxu
 * @date: 2022/6/29 9:58
 * @description:
 */
@Controller
@Slf4j
public class LoginController {
    @Autowired
    UserService userService;
    @Autowired
    Message message;
    // 访问登录页
    @GetMapping({"/","/tologin","/login"})
    public String index22(){
        return "login";
    }
    // 访问主页面
    @GetMapping({"/main.html","/main"})
    public String main(){
        return "main";
    }

    /**
     * 通过shiro安全框架进行登录
     * @param request
     * @param session
     * @return
     */
    @ResponseBody
    @PostMapping("/user/ajaxlogin")
    public Map<String, String> login(HttpServletRequest request, HttpSession session) {
        String loginname = request.getParameter("loginname");
        String password = request.getParameter("password");
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
        String rememberMe = request.getParameter("rememberMe");
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(loginname, md5password);
        log.error(rememberMe);
        if (rememberMe != null) {
            token.setRememberMe(true);
        }
        HashMap<String, String> json = new HashMap<>();
        try {

            subject.login(token); // 实行登录方法，如果没有异常就说明ok了
            session.setAttribute("loginname",loginname);
            json.put("message", "success");
            return json;

        }catch (UnknownAccountException e){// 用户名不存在
            json.put("message", "没有此用户");
            return json;
        }catch (IncorrectCredentialsException e){ // 密码不存在
            json.put("message", "账号或密码错误");
            return json;
        }
    }

    // /**
    //  * 未使用shiro安全框架前的登录验证
    //  * @param response
    //  * @param request
    //  * @param session
    //  * @return
    //  */
    // @ResponseBody
    // @PostMapping("/user/ajaxlogin")
    // public Map<String, String> login2(HttpServletResponse response,
    //                                  HttpServletRequest request, HttpSession session) {
    //     String loginname = request.getParameter("loginname");
    //     String password = request.getParameter("password");
    //     String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
    //     HashMap<String, String> json = new HashMap<>();
    //     User realUser = null;
    //     String realUserLoginname;
    //     String realUserPassword;
    //     long realUserLoginid;
    //     User user = new User();
    //     try {
    //         user.setLoginname(loginname);
    //         realUser = userService.selectUserByAny(user);
    //         realUserPassword = realUser.getPassword();
    //         realUserLoginname = realUser.getLoginname();
    //         realUserLoginid = realUser.getUserid();
    //     } catch (Exception e) {
    //         json.put("message", "没有此用户");
    //         return json;
    //     }
    //     if (loginname.equals(realUserLoginname) && realUserPassword.equals(md5password)) {
    //         session.setAttribute("loginname", loginname);
    //         session.setAttribute("loginid",realUserLoginid);
    //         json.put("message", "success");
    //         return json;
    //     } else {
    //         json.put("message", "账号或密码错误");
    //         return json;
    //     }
    // }

    @GetMapping("/user/loginUser")
    @ResponseBody
    public String getLoginUser(HttpSession session){
        String loginname = (String) session.getAttribute("loginname");
        return loginname;
    }

    @GetMapping("/user/logout")
    @ResponseBody
    public Message logout(HttpSession session){
        String loginname = (String) session.getAttribute("loginname");
        session.removeAttribute("loginname");
        message.setJson(loginname);
        return message;
    }
    @GetMapping("/loginFail")
    @ResponseBody
    public String unauthorized(){
        return "没有执行权限,请向管理员申请权限";
    }

    @GetMapping("/403")
    public String noauth(){
        return "/error/403";
    }

}
